package com.example.system.manager;

import com.example.core.constants.CacheConstants;
import com.example.redis.service.RedisService;
import com.example.system.domain.exam.Exam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 管理员竞赛内存操作
 */
@Component
public class ExamCacheManager {

    @Autowired
    private RedisService redisService;

    public void addCache(Exam exam) {
        //向redis添加(头插)
        //保存竞赛id
        redisService.leftPushForList(getExamListKey(), exam.getExamId());
        //保存竞赛基本信息
        redisService.setCacheObject(getDetailKey(exam.getExamId()), exam);
    }

    public void deleteCache(Long examId) {
        //删除指定数据
        redisService.removeForList(getExamListKey(), examId);
        //删除竞赛信息
        redisService.deleteObject(getDetailKey(examId));
    }

    private String getExamListKey() {
        return CacheConstants.EXAM_UNFINISHED_LIST;
    }

    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }

    private String getExamQuestionListKey(Long examId) {
        return CacheConstants.EXAM_QUESTION_LIST + examId;
    }
}